<!doctype html>
<html lang="en">
<head>
    {include file='public/_meta' / }
</head>
<body class="layui-card">
<div>
    <input type="text" name="" id="input"/>
    <input type="button" onclick="send()" value="发送"/>
</div>
<div id="welcome"></div>
</body>
<script>
    isApiLogin();
    let param = getParams(),
        uid = param.uid,
        wsServer = 'wss://chat.fxcyl.cn:9502?type=chat_uid_1&token=' + getApiToken(),
        webscket = null,
        lock = false;
    $(document).ready(function () {
        link();
    });

    function link() {

        webscket = new WebSocket(wsServer);

        webscket.onopen = function (res) {
            $("#welcome").append(
                "<h1>连接成功!欢迎</h1>"
            )
        }

        webscket.onclose = function (res) {
            webscket.close();
            relink()
        }

        webscket.onmessage = function (res) {
            $("#message").append(
                "<h3>" + res.data + "</h3>"
            )
        }

        webscket.onerror = function (res) {
            webscket.close();
            relink()
        }

        webscket.onmessage = function (res) {
            $("#message").append(
                "<h3>" + res.data + "</h3>"
            )
        }
    }

    function send() {
        console.log($("#input").val())
        $res = webscket.send($("#input").val());
        console.log($res)
    }

    function relink() {
        if (lock) {
            return false;
        }
        lock = true;
        setTimeout(function () {
            link();
            lock:false;
        }, 1000);
    }
</script>
</html>
